package com.banciyuan.bcywebview.biz.main.mineinfo.mark;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.CircleEnterStatistics;

/**
 * Created by squall on 2017/7/17.
 */

public class CurrentCircleSettingActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private CurrentCircleSettingAdapter currentCircleSettingAdapter;
    private List<CircleEnterStatistics> clist=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manger_circle);
        initActionbar();
        initUi();
    }

    @Override
    protected void initActionbar() {
        View mActionBarView = findViewById(R.id.base_action_bar);
        ActionbarHelper actionbarHelper = new ActionbarHelper(this, mActionBarView);
        actionbarHelper.setTitle(getString(R.string.local_like_circle));
    }

    @Override
    protected void initUi() {
        recyclerView = (RecyclerView) findViewById(R.id.recleview);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return position == 0 ? 4 : 1;
            }
        });
        recyclerView.setLayoutManager(gridLayoutManager);
        currentCircleSettingAdapter = new CurrentCircleSettingAdapter(clist, this);
        recyclerView.setAdapter(currentCircleSettingAdapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                c.drawColor(getResources().getColor(R.color.white));
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                if (parent.getAdapter() != null) {
                    if (parent.getAdapter().getItemViewType(parent.getChildAdapterPosition(view)) == 1) {
                        outRect.top = UIUtils.dip2px(24, CurrentCircleSettingActivity.this);
                    }
                }
            }
        });
        readCircleStatistics(this);
    }

    private void readCircleStatistics(final Context mContext) {
        new AsyncTask<Void, List<CircleEnterStatistics>, List<CircleEnterStatistics>>() {
            @Override
            protected List<CircleEnterStatistics> doInBackground(Void... params) {
                Gson gson = new Gson();
                String relakey = SPConstant.CIRCLESTATISTICS + UserDataHelper.getInstance(mContext).getUid();
                String data = SPHelper.getString(mContext, SPConstant.SPNAME_CIRCLESTATISTICS, relakey, "[]");
                List<CircleEnterStatistics> clist = gson.fromJson(data, new TypeToken<List<CircleEnterStatistics>>() {
                }.getType());
                Map<String, CircleEnterStatistics> map = new HashMap<>();
                for (CircleEnterStatistics c : clist) {
                    String key = c.getType() + "#" + c.getId();
                    if (map.get(key) != null) {
                        CircleEnterStatistics temp = map.get(key);
                        temp.setCount(temp.getCount() + 1);
                        temp.setEnterTime(c.getEnterTime());
                        temp.setCover(c.getCover());
                    } else {
                        CircleEnterStatistics temp = new CircleEnterStatistics(c.getName(), c.getId(), c.getEnterTime(), c.getType(), c.getCover());
                        temp.setCount(1);
                        map.put(key, temp);
                    }
                }
                List<CircleEnterStatistics> sort = new ArrayList<>();
                for (String s : map.keySet()) {
                    sort.add(map.get(s));
                }
                Collections.sort(sort, new Comparator<CircleEnterStatistics>() {
                    @Override
                    public int compare(CircleEnterStatistics lhs, CircleEnterStatistics rhs) {
                        return lhs.compareTo(rhs);
                    }
                });
                return sort;
            }

            @Override
            protected void onPostExecute(List<CircleEnterStatistics> circleEnterStatisticses) {
                clist.clear();
                clist.addAll(circleEnterStatisticses);
                if (currentCircleSettingAdapter == null) {
                    currentCircleSettingAdapter = new CurrentCircleSettingAdapter(clist, CurrentCircleSettingActivity.this);
                    recyclerView.setAdapter(currentCircleSettingAdapter);
                } else {
                    currentCircleSettingAdapter.notifyDataSetChanged();
                }
            }
        }.execute();
    }
}
